package com.lzp.customview

import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.base.BaseActivity
import com.lzp.customview.databinding.LoadMoreActivityBinding
import kotlinx.android.synthetic.main.load_more_activity.*

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/7/10
 */
class LoadMoreRvActivity : BaseActivity() {

    private val binding: LoadMoreActivityBinding by lazy {
        LoadMoreActivityBinding.inflate(
            layoutInflater
        )
    }

    private val adapter = CommentLoadMoreAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
    }

    override fun initView() {
        super.initView()
        binding.loadMoreRv.layoutManager = LinearLayoutManager(this)
        binding.loadMoreRv.adapter = adapter
        adapter.loadMoreModule.setOnLoadMoreListener {
            getNetData(false)
        }
        binding.freshLayout.setOnRefreshListener {
            getNetData(true)
        }
        binding.freshLayout.setRefresh(true)
    }

    private fun getNetData(isFresh: Boolean) {

        loadMoreRv.postDelayed({
            val list = mutableListOf<Int>()
            repeat(10) {
                list.add(it + adapter.itemCount)
            }

            if (isFresh){
                binding.freshLayout.stopRefresh()
                adapter.setList(list)
            }else{
                adapter.loadMoreModule.loadMoreComplete()
                adapter.addData(list)
            }
        }, 2000)
    }
}